package io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileDemo {
	public static void main(String args[]) throws Exception {
		// DataInputStream dis=new DataInputStream(System.in);
		// System.out.println("Enter a text:");
		/*
		 * //Reading from k/b and displaying on Monitor String
		 * str=dis.readLine(); PrintStream ps= System.out;
		 * ps.println("The text is "+ str);
		 */

		/*
		 * // File Output Stream // reading from k/b and displaying on monitor &
		 * storing a copy in a file FileOutputStream fos=new
		 * FileOutputStream("abc.txt"); int ch; while((ch=dis.read())!='@'){
		 * fos.write(ch); System.out.print((char)ch); } fos.close();
		 */

		/*
		 * // File Input Stream & File Output Stream FileInputStream fis=new
		 * FileInputStream("file1.txt"); FileOutputStream fos=new
		 * FileOutputStream("file2.txt"); int ch; while((ch=fis.read())!=-1){
		 * fos.write(ch); System.out.print((char)ch); } fis.close();
		 * fos.close();
		 */

		/*
		 * // Buffered Input Stream & Buffered Output Stream FileInputStream
		 * fis=new FileInputStream("Demo.java"); FileOutputStream fos=new
		 * FileOutputStream("file2.txt");
		 * 
		 * BufferedInputStream bis=new FileInputStream(fis);
		 * BufferedOutputStream bos=new FileOutputStream(fos);
		 * 
		 * int ch; while((ch=fis.read())!=-1){ fos.write(ch);
		 * System.out.print((char)ch); } bis.flush(); fis.close(); fos.close();
		 */

		// STORING AND RETREIVING PRIMITIVE DATA TYPES
		// Storing primitive data
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(
				"data.dat"));
		// first Employee
		dos.writeInt(100);
		dos.writeUTF("Raj");
		dos.writeDouble(12000);
		dos.writeUTF("Tester");
		// Second Employee
		dos.writeInt(101);
		dos.writeUTF("Seeta");
		dos.writeDouble(16000);
		dos.writeUTF("Programmer");
		System.out.println("Recrd stored Successfully.....");

		// Reteiving Primitive data
		DataInputStream dis = new DataInputStream(new FileInputStream(
				"data.dat"));
		// first Employee
		System.out.println("ID: " + dis.readInt());
		System.out.println("Name: " + dis.readUTF());
		System.out.println("Salary: " + dis.readDouble());
		System.out.println("Desig: " + dis.readUTF());
		// Second Employee
		System.out.println("ID: " + dis.readInt());
		System.out.println("Name: " + dis.readUTF());
		System.out.println("Salary: " + dis.readDouble());
		System.out.println("Desig: " + dis.readUTF());
		// over
		System.out.println("Done.");
	}
}